package com.ksu.manager.service.impl;

import java.util.Date;
import java.util.Map;

import com.ksu.manager.dao.IBuyLoseManagerDAO;
import com.ksu.manager.dao.IProductDAO;
import com.ksu.manager.model.BuyLoseManager;
import com.ksu.manager.model.Product;
import com.ksu.manager.model.pages.Pagination;
import com.ksu.manager.service.IBuyLoseManagerService;

public class BuyLoseManagerServiceImpl implements IBuyLoseManagerService{

	private IBuyLoseManagerDAO buyLoseManagerDAO;
	private IProductDAO productDAO;
	@Override
	public void add(BuyLoseManager buyLoseManager) {
		buyLoseManager.setStatTime(new Date());
		buyLoseManagerDAO.create(buyLoseManager);
		Product product = new Product();
		product.setId(buyLoseManager.getProductId());
        product = (Product)productDAO.queryList(product).get(0);
        product.setTotalNumber(product.getTotalNumber()+buyLoseManager.getNumber());
        product.setStatTime(new Date());
        productDAO.update(product);
		
	}

	@Override
	public void del(BuyLoseManager buyLoseManager) {
		Product product = new Product();
		product.setId(buyLoseManager.getProductId());
        product = (Product)productDAO.queryList(product).get(0);
        product.setTotalNumber(product.getTotalNumber()-buyLoseManager.getNumber());
        product.setStatTime(new Date());
        buyLoseManager.setStatTime(new Date());
        buyLoseManager.setStat("0");
        buyLoseManagerDAO.update(buyLoseManager);
		productDAO.update(product);
		
	}

	@Override
	public Map<String, Object> queryList(BuyLoseManager buyLoseManager,
			Pagination page) {
		return buyLoseManagerDAO.queryList(buyLoseManager,page);
	}

	public void setBuyLoseManagerDAO(IBuyLoseManagerDAO buyLoseManagerDAO) {
		this.buyLoseManagerDAO = buyLoseManagerDAO;
	}

	public void setProductDAO(IProductDAO productDAO) {
		this.productDAO = productDAO;
	}

	

}
